<template>
    <div class="store-fix-container">
        <div class="fix-header flex_row">
            <div class="search flex_row">
                <input type="text" class="fix-search" placeholder="请输入搜索内容" v-model="searchText"  @return="doSearch" return-key-type="search" >
                <image :src="imgPath('search.png')" class="search-img" @click="doSearch"></image>
            </div>
            <image class="shopping-car" :src="imgPath('shopping.png')" @click="toShoppingCar"></image>
        </div>
        <div class="content-wrapper flex_row">
            <!--左边导航-->
            <div class="tab-wrapper">
                <div v-if="titleLoading" class="child" v-for="(item, index) in fixList" :key="index">
                    <div class="tab-item flex_row" :class="[activeIndex === index ? 'bgc_fff' : '']" @click="changeIndex(index)">
                        <text class="tab-word" :class="[activeIndex === index && !open ? 'active-word' : '']">{{item.value}}</text>
                        <image class="triangle" v-if="item.value === '布料'" :src="imgPath(open ? 'triangle_up.png' : 'triangle_down.png')"></image>
                    </div>
                    <div class="child" v-if="index === 2 && open">
                        <div class="tab-item flex_row bgc_cecece" :class="[childIndex === indexs ? 'bgc_fff' : '']" v-for="(items, indexs) in fixList[2].treeList" :key="indexs" @click="changeChildIndex(indexs)">
                            <text class="tab-word col_464646" :class="[childIndex === indexs ? 'active-word' : '']">{{items.value}}</text>
                        </div>
                    </div>
                </div>
            </div>
            <!--右边内容区-->
            <scroller class="content" show-scrollbar="false">
                <image class="bannerImg" :src="imgPath('banner.png')"></image>
                <div v-if="fixLoading">
                    <div class="kind-wrapper" v-if="activeIndex === 0 || activeIndex === 1" v-for="(data, index) in fixedData[activeIndex]" :key="index">
                        <div class="kind-header flex_row">
                            <text class="kind-title">{{data.title}}</text>
                            <text class="checkMore" @click="pushdetail">查看全部>></text>
                        </div>
                        <div class="kind-content flex_row">
                            <div class="kind-item" v-for="(item, indexs) in data.list" :key="indexs" @click="pushStoreDeatil(item)">
                                <image class="kind-image" :src="item.topPicture ? item.topPicture : imgPath('default_image.png')"></image>
                                <text class="kind-name">{{item.goodsName}}</text>
                                <text class="kind-price">￥{{item.discountPrice ? item.discountPrice : item.price}}{{'/' + item.priceUnit}}</text>
                                <!--<div class="flex_row align_center">-->
                                    <!--<text class="col_868686 fs_24 text_line" v-if="item.discountPrice">原价￥{{item.price}}</text>-->
                                <!--</div>-->
                                <text class="discount" v-if="activeIndex === 1 && indexs === 1 && item.discount">特惠{{item.discount}}折</text>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="kind-wrapper" v-if="activeIndex > 1" v-for="(item, index) in titleList" :key="index">
                    <div class="kind-header flex_row">
                        <text class="kind-title">{{item.value}}</text>
                        <text class="checkMore" @click="checkMore(item)">查看全部>></text>
                    </div>
                    <div class="kind-content flex_row">
                        <div class="kind-item" v-for="(goods, indexs) in contentData[index]" :key="indexs" @click="pushStoreDeatil(goods)">
                            <image class="kind-image" :src="goods.topPicture ? goods.topPicture : imgPath('default_image.png')"></image>
                            <text class="kind-name">{{goods.goodsName}}</text>
                            <text class="kind-price">￥{{goods.discountPrice ? goods.discountPrice : goods.price}}{{'/' + goods.priceUnit}}</text>
                            <!--<div class="flex_row align_center">-->
                                <!--<text class="kind-price">￥{{goods.discountPrice ? goods.discountPrice : goods.price}}</text>-->
                                <!--<text class="col_868686 fs_24 text_line" v-if="goods.discountPrice">原价￥{{goods.price}}</text>-->
                            <!--</div>-->
                        </div>
                    </div>
                </div>
            </scroller>
        </div>
        <!--底部导航-->
        <navBottom></navBottom>
        <!--loading-->
        <wxc-loading :show="loadingShow" :need-mask="needMask"></wxc-loading>
    </div>
</template>

<script>
import navBottom from '@/components/navBottom'
import { storeIndex, storeOtherType, shopGoodsList } from '@/api/store'
import { WxcLoading } from 'weex-ui'
const storage = weex.requireModule('storage')
// const modal = weex.requireModule('modal')
export default {
  name: 'store',
  data () {
    return {
      activeIndex: 0, // 当前active下标
      childIndex: 0, // 子类下标
      length: 0, // 长度
      fixedData: [
        [
          {
            title: '最新',
            list: []
          },
          {
            title: '畅销',
            list: []
          }
        ],
        [
          {
            title: '优质精选',
            list: []
          },
          {
            title: '特价爆销',
            list: []
          }
        ]
      ], // 推荐、活动专区
      titleLoading: false, // 标题是否加载完
      fixLoading: false, // 推荐、活动是否加载完
      fixList: [
        { value: '推荐专区', type: 10 },
        { value: '活动专区 ', type: 9 }
      ], // 固定位置
      titleList: [], // 右边标题列表
      contentData: [], // 右边数据
      open: false, // 是否展开
      pageNo: 1,
      pageSize: 2,
      newestList: [], // 最新
      bestSellingList: [], // 畅销
      selectedList: [], // 优质精选
      specialOfferList: [], // 特价爆销
      searchText: '', // 搜索绑定的字段
      needMask: true, // 显示蒙版
      loadingShow: true // loading
    }
  },
  mounted () {
    const that = this
    // that.activeIndex = that.$route.query.index ? that.$route.query.index : 0
    that.loadingShow = true
    that.$api.get(storeOtherType + '/0', {}, function (res) {
      that.fixList = that.fixList.concat(res.data)
      storage.getItem('storeIndex', event => {
        if (event.result === 'success') {
          that.changeIndex(parseInt(event.data))
          storage.removeItem('storeIndex', event => {})
        }
      })
      that.titleLoading = true
      that.$api.get(storeIndex + '/' + that.pageNo + '/' + that.pageSize, {}, function (res) {
        that.fixedData[0][0].list = res.data.newestList.records // 最新
        that.fixedData[0][1].list = res.data.bestSellingList.records // 畅销
        that.fixedData[1][0].list = res.data.selectedList.records // 优质精选
        that.fixedData[1][1].list = res.data.specialOfferList.records // 特价
        that.loadingShow = false
        that.fixLoading = true
      }, function () {
        that.loadingShow = false
      })
    }, function () {
      that.loadingShow = false
    })
  },
  methods: {
    toShoppingCar () { // 跳转购物车
      this.$router.push('/store/shoppingCar')
    },
    changeIndex (inx) { // 切换类型
      const that = this
      if (inx === 2) {
        if (that.activeIndex === inx) {
          that.open = !that.open
          return
        }
        that.activeIndex = inx
        that.open = true
        that.childIndex = 0
        that.titleList = that.fixList[that.activeIndex].treeList[that.childIndex].treeList
        that.contentData = []
        that.length = 0
        that.queryData()
      } else {
        if (that.activeIndex === inx) {
          return
        }
        that.activeIndex = inx
        that.open = false
        that.childIndex = -1
        if (inx > 2) {
          that.titleList = that.fixList[inx].treeList
          that.contentData = []
          that.length = 0
          that.queryData()
        }
      }
    },
    changeChildIndex (inx) { // 子类切换
      const that = this
      if (that.childIndex === inx) {
        return
      }
      that.childIndex = inx
      that.titleList = that.fixList[that.activeIndex].treeList[that.childIndex].treeList
      that.contentData = []
      that.length = 0
      that.queryData()
    },
    queryData () {
      const that = this
      if (that.length >= that.titleList.length) {
        return
      }
      let params = {
        type: that.titleList[that.length].id,
        pageNo: that.pageNo,
        pageSize: that.pageSize
      }
      that.loadingShow = true
      that.$api.get(shopGoodsList, params, function (res) {
        that.loadingShow = false
        that.contentData.push(res.data.records)
        that.length++
        that.queryData()
      }, function () {
        that.loadingShow = false
      })
    },
    checkMore (item) { // 查看更多
      const that = this
      storage.setItem('storeIndex', that.activeIndex, (event) => {
        if (event.result === 'success') {
          that.$router.push({path: 'store/storeType', query: {type: item.id, searchText: '', value: item.value, item: item}})
        }
      })
    },
    // 这个是活动专区和推荐专区查看更多
    pushdetail () {
      const that = this
      storage.setItem('storeIndex', that.activeIndex, (event) => {
        if (event.result === 'success') {
          that.$router.push({path: 'store/storeType', query: {type: '', item: '', searchText: ''}})
        }
      })
    },
    pushStoreDeatil (item) { // 商品详情
      const that = this
      storage.setItem('storeIndex', that.activeIndex, (event) => {
        if (event.result === 'success') {
          that.$router.push({path: 'store/storeDetail', query: {goodsId: item.id}})
        }
      })
    },
    doSearch () { // 搜索
      const that = this
      that.$router.push({path: '/store/storeType', query: {searchText: that.searchText, item: '', type: ''}})
    }
  },
  components: {
    navBottom,
    WxcLoading
  }
}
</script>

<style scoped>
    .flex_row {
        flex-direction: row;
    }
    .align_center {
        align-items: center;
    }
    .col_cecece {
        color: #cecece;
    }
    .col_262626 {
        color: #262626;
    }
    .col_464646 {
        color: #464646;
    }
    .col_868686 {
        color: #868686;
    }
    .text_line {
        margin-left: 12px;
        text-decoration: line-through;
    }
    .bgc_cecece {
        background-color: rgb(240,240,240);
    }
    .fs_24 {
        font-size: 24px;
    }
    .fs_26 {
        font-size: 26px;
    }
    .fs_30 {
        font-size: 30px;
    }
    .bgc_fff {
        background-color: #fff;
    }
    .fix-header {
        height: 88px;
        padding-left: 24px;
        padding-right: 24px;
        background-image: linear-gradient(to right, #2b3040, #7d88a4);
        justify-content: space-between;
        align-items: center;
    }
    .search {
        width: 622px;
        height: 60px;
        padding-left: 16px;
        padding-right: 16px;
        align-items: center;
        border-radius: 10px;
        background-color: #fff;
    }
    .search-img {
        width: 28px;
        height: 28px;
    }
    .fix-search {
        width: 546px;
        height: 60px;
        border: none;
        font-size: 24px;
        color: #464646;
        placeholder-color: #cecece;
    }
    .shopping-car {
        width: 42px;
        height: 42px;
    }
    .content-wrapper {
        position: absolute;
        top: 88px;
        bottom: 98px;
        left: 0;
        right: 0;
        /*height: 1334px;*/
        /*margin-bottom: 140px;*/
    }
    .tab-wrapper {
        width: 162px;
        /*background-color: #f5f5f5;*/
        background-color: rgb(230,230,230);
    }
    .tab-item {
        height: 88px;
        padding-top: 28px;
        padding-bottom: 28px;
        flex-wrap: wrap;
    }
    .tab-word {
        /*width: 162px;*/
        height: 32px;
        padding-left: 24px;
        font-size: 26px;
        color: #868686;
        /*text-align: center;*/
    }
    .triangle {
        width: 18px;
        height: 12px;
        margin-top: 8px;
        margin-left: 16px;
    }
    .active-word {
        padding-left: 18px;
        border-left-width: 6px;
        border-left-style: solid;
        border-left-color: #fecb01;
        font-size: 30px;
        color: #262626;
    }
    .content {
        width: 588px;
        padding-top: 24px;
        padding-left: 24px;
        padding-right: 24px;
        padding-bottom: 30px;
        /*padding-bottom: 140px;*/
    }
    .bannerImg {
        width: 540px;
        height: 200px;
        border-radius: 10px;
    }
    .kind-wrapper {
        margin-top: 8px;
    }
    .kind-header {
        height: 56px;
        align-items: center;
        justify-content: space-between;
    }
    .kind-title {
        font-size: 24px;
        font-weight: bold;
        color: #262626;
    }
    .checkMore {
        font-size: 24px;
        color: #ff0f37;
    }
    .kind-content {
        justify-content: space-between;
    }
    .kind-item {
        width: 258px;
        height: 372px;
        border-width: 1px;
        border-style: solid;
        border-color: #ebebeb;
        border-radius: 20px;
    }
    .kind-image {
        width: 256px;
        height: 256px;
    }
    .kind-name {
        padding-top: 12px;
        padding-left: 8px;
        padding-right: 8px;
        lines: 1;
        line-height: 48px;
        font-size: 24px;
        color: #464646;
        font-weight: bold;
        text-overflow: ellipsis;
    }
    .kind-price {
        padding-left: 8px;
        padding-right: 8px;
        /*padding-bottom: 20px;*/
        line-height: 36px;
        color: #fecb01;
        font-weight: bold;
        font-size: 28px;
    }
    .child {
        width: 162px;
    }
    .discount {
        position: absolute;
        right: 1px;
        top: 16px;
        width: 120px;
        height: 40px;
        color: #fff;
        font-size: 24px;
        line-height: 40px;
        font-weight: bold;
        text-align: center;
        background-color: #ff0f37;
    }
</style>
